package sg.edu.nus.iss.billing.gui.subscription;

import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.BTN_H;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.BTN_W;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.CONTENT_H;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.CONTENT_W;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.LEFT_MARGIN;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.TXT_H;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.TXT_W;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.getXPosition;
import static sg.edu.nus.iss.billing.gui.subscription.UIConstants.getYPosition;
import static sg.edu.nus.iss.billing.localization.MessageKeyManager.getMessage;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import sg.edu.nus.iss.billing.entity.service.PackageService;
import sg.edu.nus.iss.billing.entity.service.Service;
import sg.edu.nus.iss.billing.entity.service.SubscriptionEntity;
import sg.edu.nus.iss.billing.entity.service.ValueAddedService;
import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
import sg.edu.nus.iss.billing.validation.ValidationRule;
/**
 * 
 * @author RAJU NAVANEETHAM VENKATESHWARI
 *
 */
public abstract class SubscriptionTemplatePanel extends JPanel {

	private static final long serialVersionUID = -4727709193755639255L;

	private JLabel addiSvcLbl;

	private ServiceViewPanel addiSvcViewPanel;

	private ButtonGroup subscribedRadios;

	private JLabel basicSvcLbl;

	private ServiceViewPanel basicSvcViewPanel;

	private JButton cancelBtn;

	private JButton desubsBtn;

	private JLabel planSubscribedDtLbl;

	private ValidateJTextField planSubscribedDtTxt;

	private ValidateJTextField svcNoTxt;

	private JLabel svcNoNameLbl;

	private JLabel planTerminateDtLbl;

	private ValidateJTextField planTerminateDtTxt;

	private JButton saveBtn;

	private JButton phoneNoGenBtn;

	private JLabel subscribedLbl;

	private JLabel subsFeeLbl;

	private JLabel subsFeeTxt;

	private JLabel rateTypeTxt;

	private JLabel rateTxt;

	private boolean showBasicSvc = true;

	private boolean isViewModel = false;

	private boolean isRateable = false;

	private boolean isChoicable = false;

	private boolean isCallable = false;

	private List<PackageService> displayPakSvcs = new ArrayList<PackageService>();

	private Map<String, SubscriptionEntity> subscribedVAS = new HashMap<String, SubscriptionEntity>();

	private Map<String, ValueAddedService> unsibscribedVAS = new HashMap<String, ValueAddedService>();

	private Map<String, SubscriptionEntity> subscribedBasicSvc = new HashMap<String, SubscriptionEntity>();

	private Map<String, ValueAddedService> unsibscribedBasicSvc = new HashMap<String, ValueAddedService>();

	protected void initGUI() {

		int row = 0;
		this.setLayout(null);
		setPreferredSize(new Dimension(CONTENT_W, CONTENT_H));

		this.add(getSubscribedLbl());
		getSubscribedLbl().setText(getMessage("label.subs.svcpak"));
		getSubscribedLbl().setBounds(getXPosition(1), getYPosition(++row), TXT_W, TXT_H);

		int col = 0;

		col = 2;
		int i = 0;
		for (Service pkg : displayPakSvcs) {
			if (i!=0&&i%3==0) {
				row++;
				col = 2;
			}
			JRadioButton subscribedBtn = new JRadioButton(pkg.getServiceName());
			subscribedBtn.setActionCommand(pkg.getServiceName());
			subscribedBtn.setBounds(getXPosition(col), getYPosition(row), TXT_W, TXT_H);
			subscribedBtn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					handlePlanSwitch(e);
				}
			});
			getSubscribedRadios().add(subscribedBtn);
			this.add(subscribedBtn);
			col++;
			i++;
		}

		if (isCallable) {
			getSvcNoNameLbl().setText(getMessage("label.subs.phoneno"));
			getSvcNoNameLbl().setBounds(getXPosition(1), getYPosition(++row), TXT_W, TXT_H);
			this.add(getSvcNoNameLbl());

			getSvcNoTxt().setBounds(getXPosition(2), getYPosition(row), TXT_W, TXT_H);
			this.add(getSvcNoTxt());

			getPhoneNoGenBtn().setText(getMessage("label.subs.phoneNoGen"));
			getPhoneNoGenBtn().setBounds(getXPosition(3), getYPosition(row), BTN_W, BTN_H);
			this.add(getPhoneNoGenBtn());
		}

		getSubsFeeLbl().setText(getMessage("label.subs.subsFee"));
		getSubsFeeLbl().setBounds(getXPosition(1), getYPosition(++row), TXT_W, TXT_H);
		this.add(getSubsFeeLbl());

		this.add(getSubsFeeTxt());
		getSubsFeeTxt().setText("0.0");
		getSubsFeeTxt().setBounds(getXPosition(2), getYPosition(row), TXT_W, TXT_H);

		this.add(getPlanSubscribedDtLbl());
		getPlanSubscribedDtLbl().setText(getMessage("service.commencedDt"));
		getPlanSubscribedDtLbl().setBounds(getXPosition(1), getYPosition(++row), TXT_W, TXT_H);

		getPlanSubscribedDtTxt().setBounds(getXPosition(2), getYPosition(row), TXT_W, TXT_H);
		this.add(getPlanSubscribedDtTxt());
		
		getPlanTerminateDtLbl().setText(getMessage("service.terminateDt"));
		getPlanTerminateDtLbl().setBounds(getXPosition(3), getYPosition(row), TXT_W, TXT_H);
		this.add(getPlanTerminateDtLbl());
		

		getPlanTerminateDtTxt().setBounds(getXPosition(4), getYPosition(row), TXT_W, TXT_H);
		this.add(getPlanTerminateDtTxt());
		

		int svcViewPanelHight = 0;
		int svcViewPanelWidth = CONTENT_W-LEFT_MARGIN*2;
		if (showBasicSvc) {
			getBasicSvcLbl().setText(getMessage("label.subs.basicSVC"));
			getBasicSvcLbl().setBounds(getXPosition(1), getYPosition(++row), TXT_W, TXT_H);
			this.add(getBasicSvcLbl());
			
			int rowCount = subscribedBasicSvc.size()+unsibscribedBasicSvc.size();
			if (rowCount<2)
				rowCount = 2;
			svcViewPanelHight = (rowCount+1)*TXT_H+10;
			getBasicSvcViewPanel().setIsViewModel(isViewModel);
			getBasicSvcViewPanel().setBasicChociable(isChoicable);
			getBasicSvcViewPanel().setIsBasicSvc(true);
			getBasicSvcViewPanel().setBounds(getXPosition(1), getYPosition(++row), svcViewPanelWidth, svcViewPanelHight);
			this.add(getBasicSvcViewPanel());

		}
		int skip = svcViewPanelHight;
		
		
		getAddiSvcLbl().setText(getMessage("label.subs.addiSVC"));
		getAddiSvcLbl().setBounds(getXPosition(1), getYPosition(row, skip), TXT_W, TXT_H);
		this.add(getAddiSvcLbl());

		int rowCount = subscribedVAS.size()+unsibscribedVAS.size();
		svcViewPanelHight = (rowCount+1)*TXT_H+10;
		getAddiSvcViewPanel().setIsViewModel(isViewModel);
		getAddiSvcViewPanel().setIsBasicSvc(false);
		getAddiSvcViewPanel().setBounds(getXPosition(1), getYPosition(++row, skip), svcViewPanelWidth, svcViewPanelHight);
		this.add(getAddiSvcViewPanel());
		
	}
	
	protected abstract void handlePlanSwitch(ActionEvent e);

	protected JLabel getAddiSvcLbl() {

		if (addiSvcLbl==null) {
			addiSvcLbl = new JLabel();
		}
		return addiSvcLbl;
	}

	protected ServiceViewPanel getAddiSvcViewPanel() {

		if (addiSvcViewPanel==null) {
			addiSvcViewPanel = new ServiceViewPanel();
		}
		return addiSvcViewPanel;
	}

	protected ButtonGroup getSubscribedRadios() {

		if (subscribedRadios==null) {
			subscribedRadios = new ButtonGroup();
		}
		return subscribedRadios;
	}

	protected JLabel getBasicSvcLbl() {

		if (basicSvcLbl==null) {
			basicSvcLbl = new JLabel();
		}
		return basicSvcLbl;
	}

	protected ServiceViewPanel getBasicSvcViewPanel() {

		if (basicSvcViewPanel==null) {
			basicSvcViewPanel = new ServiceViewPanel();
		}
		return basicSvcViewPanel;
	}

	protected JButton getCancelBtn() {

		if (cancelBtn==null) {
			cancelBtn = new JButton();
		}
		return cancelBtn;
	}

	protected JButton getDesubsBtn() {

		if (desubsBtn==null) {
			desubsBtn = new JButton();
		}
		return desubsBtn;
	}
	

	
	protected void setSubscribedRadios(ButtonGroup subscribedRadios) {
		
		this.subscribedRadios = subscribedRadios;
	}

	protected JLabel getPlanSubscribedDtLbl() {

		if (planSubscribedDtLbl==null) {
			planSubscribedDtLbl = new JLabel();
		}
		return planSubscribedDtLbl;
	}

	protected ValidateJTextField getPlanSubscribedDtTxt() {

		if (planSubscribedDtTxt==null) {
			planSubscribedDtTxt = new ValidateJTextField(getMessage("service.commencedDt"),ValidationRule.NOT_NULL,ValidationRule.GREATER_THAN_TODAY);
		}
		return planSubscribedDtTxt;
	}

	protected ValidateJTextField getSvcNoTxt() {

		if (svcNoTxt==null) {
			svcNoTxt = new ValidateJTextField(getMessage("label.subs.phoneno"), ValidationRule.NOT_NULL, ValidationRule.MOBILE);
		}

		return svcNoTxt;
	}

	protected JLabel getSvcNoNameLbl() {

		if (svcNoNameLbl==null) {
			svcNoNameLbl = new JLabel();
		}

		return svcNoNameLbl;
	}

	protected JLabel getPlanTerminateDtLbl() {

		if (planTerminateDtLbl==null) {
			planTerminateDtLbl = new JLabel();
		}

		return planTerminateDtLbl;
	}

	protected ValidateJTextField getPlanTerminateDtTxt() {

		if (planTerminateDtTxt==null) {
			planTerminateDtTxt = new ValidateJTextField(getMessage("service.terminateDt"), ValidationRule.GREATER_THAN_TODAY);
			planTerminateDtTxt.setText("");
		}
		return planTerminateDtTxt;
	}

	protected JButton getSaveBtn() {

		if (saveBtn==null) {
			saveBtn = new JButton();
		}
		return saveBtn;
	}

	protected JButton getPhoneNoGenBtn() {

		if (phoneNoGenBtn==null) {
			phoneNoGenBtn = new JButton();
		}
		return phoneNoGenBtn;
	}

	protected JLabel getSubscribedLbl() {

		if (subscribedLbl==null) {
			subscribedLbl = new JLabel();
		}
		return subscribedLbl;
	}

	protected JLabel getSubsFeeLbl() {

		if (subsFeeLbl==null) {
			subsFeeLbl = new JLabel();
		}
		return subsFeeLbl;
	}

	protected JLabel getSubsFeeTxt() {

		if (subsFeeTxt==null) {
			subsFeeTxt = new JLabel();
		}
		return subsFeeTxt;
	}

	protected JLabel getRateTypeTxt() {

		if (rateTypeTxt==null) {
			rateTypeTxt = new JLabel();
		}
		return rateTypeTxt;
	}

	protected JLabel getRateTxt() {

		if (rateTxt==null) {
			rateTxt = new JLabel();
		}
		return rateTxt;
	}

	protected boolean isShowBasicSvc() {

		return showBasicSvc;
	}

	protected boolean isViewModel() {

		return isViewModel;
	}

	protected boolean isRateable() {

		return isRateable;
	}

	protected boolean isChoicable() {

		return isChoicable;
	}

	protected boolean isCallable() {

		return isCallable;
	}

	protected void setViewModel(boolean isViewModel) {

		this.isViewModel = isViewModel;
	}

	protected void setRateable(boolean isRateable) {

		this.isRateable = isRateable;
	}

	protected void setChoicable(boolean isChoicable) {

		this.isChoicable = isChoicable;
	}

	protected void setCallable(boolean isCallable) {

		this.isCallable = isCallable;
	}

	protected List<PackageService> getDisplayPakSvcs() {

		return displayPakSvcs;
	}

	protected void setDisplayPakSvcs(List<PackageService> displayPakSvcs) {

		this.displayPakSvcs = displayPakSvcs;
	}

	protected Map<String, SubscriptionEntity> getSubscribedVAS() {

		return subscribedVAS;
	}

	protected void setSubscribedVAS(Map<String, SubscriptionEntity> subscribedVAS) {

		this.subscribedVAS = subscribedVAS;
	}

	protected Map<String, ValueAddedService> getUnsibscribedVAS() {

		return unsibscribedVAS;
	}

	protected void setUnsibscribedVAS(Map<String, ValueAddedService> unsibscribedVAS) {

		this.unsibscribedVAS = unsibscribedVAS;
	}

	protected Map<String, SubscriptionEntity> getSubscribedBasicSvc() {

		return subscribedBasicSvc;
	}

	protected void setSubscribedBasicSvc(Map<String, SubscriptionEntity> subscribedBasicSvc) {

		this.subscribedBasicSvc = subscribedBasicSvc;
	}

	protected Map<String, ValueAddedService> getUnsibscribedBasicSvc() {

		return unsibscribedBasicSvc;
	}

	protected void setUnsibscribedBasicSvc(Map<String, ValueAddedService> unsibscribedBasicSvc) {

		this.unsibscribedBasicSvc = unsibscribedBasicSvc;
	}

}
